<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index() {
    	if(session('ADMIN_ID')&&session('ADMIN_NAME')){
            $this -> success('您已经登录 - '.session('ADMIN_NAME'),U('Index/index'));
        }else{
        	$this -> display();
        }
    }
    public function login() {
        $Admin = D("Admin");
        
        if (!$Admin->validate($rules)->create()){   
            $this->error($Admin->getError(),'',2);
        }
        $verify = I("post.verify");
        if (empty($verify)) {
            $this -> error("请填写验证码");
        }
        if(!($this -> check_verify($verify))){
            $this -> error("验证码错误");
        }
        
        $pwd = $Admin -> admin_pwd;
        $result = $Admin -> where("admin_name='{$Admin->admin_name}'") -> find();
        if ($result) {
            if ($result['admin_pwd'] == md5($pwd)) { //$Admin->admin_pwd
            	if(!$result['status']){
            		$this -> error('账号处于锁定状态');
            	}else{
            		session(C('AUTH_KEY'), $result["id"]);
            		session("ADMIN_NAME", $result["admin_name"]);
	                session("nickname", $result["nickname"]);
	                session("role", $result['role']);
	                $result['last_ip'] = get_client_ip();
	                $result['last_time'] = date("Y-m-d H:i:s");
	                $Admin -> save($result);
	                $this -> success("登录成功,{$result["nickname"]}", U("Index/index"));
            	}
                
            } else {
                $this -> error("密码错误");
            }
        } else {
            $this -> error("没有这个账号");
        }
    }

    public function logout() {
        session(null);
        $this -> success("您已经退出", "index");

    }

    public function verify() {
        $config = array(
            'imageW'        => 290,
            'imageH'        => 60,
            'fontSize'      => 30,              // 验证码字体大小
            'length'        => 4,               // 验证码位数    
            'useNoise'      => true,            // 验证码杂点
            'codeSet'       => '1234567890',    // 字符集
            //'fontttf'       => 'yegenyou.ttf',
            //'useZh'         => true,
        );
        $verify = new \Think\Verify($config);
        $verify -> entry();
    }

    function check_verify($code, $id = "") {
        $verify = new \Think\Verify();
        return $verify -> check($code, $id);
    }

}
